﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SpiderHelper.Tools.MMOnly
{
    /// <summary>
    /// 读取一个相册
    /// </summary>
    public class AlbumRead
    {
        Common.Net.Request request = null;
        List<string> albumPages = null;
        RohonModels.Models.Album album = null;
        private string baseurl = string.Empty;
        internal AlbumRead(RohonModels.Models.Album album)
        {
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            request = new Common.Net.Request(container);

            albumPages = new List<string>();
            this.album = album;
        }

        internal string Read(string url, bool next, out string imageURL)
        {
            if (string.IsNullOrEmpty(baseurl))
            {
                baseurl = url;
            }

            if (url.IndexOf(baseurl.Replace(".html", "")) < 0)//换了一个相册了
            {
                imageURL = "";
                return "";
            }

            string nexturl = "";
            string pageString = "";
            try
            {
                pageString = request.ReadUrlContent(url, MMVisitor.encode);
            }
            catch (Exception exp)
            {
                ("读取网页失败: " + url).Info();
            }
            if (!string.IsNullOrEmpty(pageString))
            {
                string message = "访问网页 " + url;
                //RohonOutPut.OutPut.Report.OutPut(message);
                string nextURL = "";
                string tempStr = "";
                if (pageString.IndexOf("<div class=\"arcBody\">") > 0)
                {
                    string str = pageString.Substring(pageString.IndexOf("<div class=\"arcBody\">") + 21);
                    str = str.Substring(0, str.IndexOf("<div class=\"hr10\"></div>"));
                    tempStr = str;
                    if (str.IndexOf("<img src=\"") > 0)
                    {
                        string t = str.Substring(0, str.IndexOf("<img src=\""));

                        bool hasNext = false;
                        if (t.IndexOf("<a href") > 0)
                        {
                            hasNext = true;

                        }
                        string imgstr = str.Substring(str.IndexOf("<img src=\""));
                        imgstr = imgstr.Substring(0, imgstr.IndexOf("/>") + 2);

                        string kzm = imgstr.Substring(imgstr.LastIndexOf("."));

                        nextURL = tempStr.Substring(tempStr.IndexOf("<a href='") + 9);
                        nextURL = nextURL.Substring(0, nextURL.IndexOf("'"));

                        string nextpageString = pageString.Substring(pageString.IndexOf("<div class=\"arcLocal r\">") + 25);
                        nextpageString = nextpageString.Substring(0, nextpageString.IndexOf("</div>"));

                        string baseRoot = album.Url.Substring(album.Url.LastIndexOf("/"));
                        //if (nextURL.StartsWith("/"))
                        //{
                        if (!hasNext)
                        {

                            GoToNewAlbum(next, baseRoot, ref nextURL, ref nextpageString);
                        }
                        else
                        {
                            if (nextURL.StartsWith(baseRoot))
                            {
                                GoToNewAlbum(next, baseRoot, ref nextURL, ref nextpageString);
                            }
                            else
                            {
                                nextURL = url.Substring(0, url.LastIndexOf("/") + 1) + nextURL;
                            }

                        }
                        //}
                        //else
                        //{
                        //    nextURL = url.Substring(0, url.LastIndexOf("/") + 1) + nextURL;
                        //}


                        //  imgstr.Debug();
                        //     RohonOutPut.OutPut.Report.OutPut("发现图片 " + imgstr);
                        if (imgstr.IndexOf("http:") < 0)
                        {
                            imgstr = MMVisitor.URL + imgstr;
                        }

                        imageURL = imgstr;

                        nexturl = nextURL;
                    }
                    else
                    {
                        imageURL = "";
                        nexturl = "";
                    }
                }
                else
                {
                    imageURL = "";
                    nexturl = "";
                }
            }
            else
            {
                imageURL = "";
                nexturl = "";
            }
            return nexturl;
        }

        private static void GoToNewAlbum(bool next, string baseRoot, ref string nextURL, ref string nextpageString)
        {
            nextURL = MMVisitor.URL + nextURL.Substring(1);//这里应该是读取上一个相册或者下一个相册
            if (next)
            {
                //下一个相册
                nextpageString = nextpageString.Substring(nextpageString.IndexOf("<a href='") + 9);
                nextpageString = nextpageString.Substring(nextpageString.IndexOf("<a href='") + 9);
                nextpageString = nextpageString.Substring(0, nextpageString.IndexOf("'"));
                //  RohonOutPut.OutPut.Report.OutPut("访问下一个相册");
                if (nextpageString.StartsWith("/"))
                {
                    if (nextpageString.StartsWith(baseRoot))
                    {
                        nextURL = MMVisitor.URL.Substring(0, MMVisitor.URL.Length - 1) + nextpageString;
                    }
                    else
                    {
                        nextURL = MMVisitor.URL.Substring(0, MMVisitor.URL.Length - 1) + baseRoot + nextpageString;
                    }
                }
                else
                {
                    nextURL = nextpageString;
                }
            }
            else
            {
                //上一个相册
                nextpageString = nextpageString.Substring(nextpageString.IndexOf("<a href='") + 9);
                nextpageString = nextpageString.Substring(0, nextpageString.IndexOf("'"));
                //  RohonOutPut.OutPut.Report.OutPut("访问上一个相册");
                if (nextpageString.StartsWith("/"))
                {
                    //   string _tempurl = url.Substring(0, url.LastIndexOf("/") - 1);
                    //   _tempurl = _tempurl.Substring(0, _tempurl.LastIndexOf("/"));
                    if (nextpageString.StartsWith(baseRoot))
                    {
                        nextURL = MMVisitor.URL.Substring(0, MMVisitor.URL.Length - 1) + nextpageString;
                    }
                    else
                    {
                        nextURL = MMVisitor.URL.Substring(0, MMVisitor.URL.Length - 1) + baseRoot + nextpageString;
                    }
                }
                else
                {
                    nextURL = nextpageString;
                }

            }
            //  RohonOutPut.OutPut.Report.OutPut("读取下一个相册:" + nextURL);
        }

        public void GetPages(string str, string url)
        {
            string pages = str.Substring(str.IndexOf("<div class=\"page\">") + 18);
            pages = pages.Substring(0, pages.IndexOf("</div>"));



            string[] pagearray = pages.Split(new string[] { "</li><li" }, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 3; i < pagearray.Length - 1; i++)
            {
                string pager = pagearray[i].Substring(pagearray[i].IndexOf("href='") + 6);
                pager = pager.Substring(0, pager.IndexOf("'"));
                pager = url.Substring(0, url.LastIndexOf("/") + 1) + pager;
                bool isInAlbum = false;
                foreach (string s in albumPages)
                {
                    if (s.Equals(pager))
                    {
                        isInAlbum = true;
                    }
                }
                if (!isInAlbum)
                {
                    albumPages.Add(pager);
                }
                //pager.Debug();
            }



        }

    }
}
